<?php

namespace app\controller\admin;

use app\base\BaseEncryptController;
use app\libs\Aes;
use app\model\store\StoreAuthGroupAccessModel;
use app\model\store\StoreAuthGroupModel;
use app\model\store\StoreAuthRuleModel;
use app\model\store\StoreAuthUserModel;
use Firebase\JWT\JWT;
use think\facade\Config;
use think\Request;

class StoreAuthUser extends BaseEncryptController
{
    protected function initialize()
    {
        parent::initialize();
        $this->model = new StoreAuthUserModel();
    }

    /**
     * 分页列表
     * @param string $group_id
     * @param string $status
     * @param string $keyword
     * @param int $page
     * @param int $limit
     * @return \think\response\Json
     */
    public function getPage($group_id = '', $status = '', $keyword = '', $page = 1, $limit = 10)
    {
        $data = $this->model->getPageData($group_id, $status, $keyword, $page, $limit, 'a.id,a.username,a.realname,a.avatar,a.status,a.create_time,a.store_id,c.title as group_title,s.store_name');
        return self::returnMsg(1, [
            'totalCount' => $data['total'],
            'currentPage' => $data['current'],
            'pageList' => $data['dataList']
        ]);
    }

    /**
     * 单条信息
     * @param $id
     * @return \think\response\Json
     */
    public function getInfo($id)
    {
        $user = $this->model->getInfo($id, 'id,username,phone,realname,avatar,status,store_id', ['roleAccess']);
        if (!empty($user)) {
            return self::returnMsg(1, $user);
        } else {
            return self::returnMsg(1001);
        }
    }

    /**
     * 用户信息
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getProfile()
    {
        $user = $this->model->getInfo($this->currentUser['user_id'], 'id,username,realname,avatar', ['roleAccess']);
        if (!empty($user)) {
            $admin_name = Config::get('system.administrator');
            $is_admin = $admin_name == $user['username'];
            $rules = '';
            if (!$is_admin && !empty($user['group_id'])) {
                $db_auth_group = new StoreAuthGroupModel();
                $role = $db_auth_group->getInfoByRedis($user['group_id']);
                $rules = $role['rules'];
            }
            $db_rule = new StoreAuthRuleModel();
            $ruleInfo = $db_rule->getAuthMenu($rules, $is_admin);
            return self::returnMsg(1, [
                'realname' => $user['realname'],
                'avatar' => $user['avatar'],
                'menuTree' => $ruleInfo['menuTree'],
                'permission' => $ruleInfo['permission']
            ]);
        } else {
            return self::returnMsg(1001);
        }
    }

    /**
     * 保存
     * @param Request $request
     * @return \think\response\Json
     * @throws \Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function onSave(Request $request)
    {
        $formData = [
            'id' => $request->param('id'),
            'username' => $request->param('username'),
            'password' => $request->param('password'),
            'phone' => $request->param('phone'),
            'realname' => $request->param('realname'),
            'status' => $request->param('status'),
            'group_id' => $request->param('group_id'),
            'store_id' => $request->param('store_id'),
        ];
        if (empty($formData['id'])) {
            $validate = 'app\validate\AuthUserValidate.add';
            if (empty($formData['password'])) {
                $default_pwd = Config::get('system.default_pwd');
                $formData['password'] = password_hash($default_pwd, PASSWORD_BCRYPT);
            } else {
                $formData['password'] = password_hash($formData['password'], PASSWORD_BCRYPT);
            }
        } else {
//            $validate = 'app\validate\AuthUserValidate.edit';
            if (!empty($formData['password'])) {
                $formData['password'] = password_hash($formData['password'], PASSWORD_BCRYPT);
            } else {
                unset($formData['password']);
            }
            $ret = $this->model->saveData($formData);
            if (false !== $ret['flag']) {
                if (!empty($formData['group_id'])) {
                    $user_id = $ret['id'];
                    $db_access = new StoreAuthGroupAccessModel();
                    $db_access->where('user_id', $user_id)->delete(true);
                    $db_access->saveData([
                        'user_id' => $user_id,
                        'group_id' => $formData['group_id']
                    ]);
                }
                $this->model->delRedis($ret['id']);
                return self::returnMsg(1);
            } else {
                return self::returnMsg(0);
            }
        }
        $result = $this->validate($formData, $validate);
        if (true !== $result) {
            return self::returnMsg(991, [], $result);
        } else {
            $ret = $this->model->saveData($formData);
            if (false !== $ret['flag']) {
                if (!empty($formData['group_id'])) {
                    $user_id = $ret['id'];
                    $db_access = new StoreAuthGroupAccessModel();
                    $db_access->where('user_id', $user_id)->delete(true);
                    $db_access->saveData([
                        'user_id' => $user_id,
                        'group_id' => $formData['group_id']
                    ]);
                }
                $this->model->delRedis($ret['id']);
                return self::returnMsg(1);
            } else {
                return self::returnMsg(0);
            }
        }
    }

    /**
     * 删除
     * @param $id
     * @return \think\response\Json
     */
    public function onDelete($id)
    {
        if ($id == $this->currentUser['user_id']) {
            return self::returnMsg(1002);
        }
        $flag = $this->model::destroy(['id' => $id]);
        if (false !== $flag) {
            $this->model->delRedis($id);
            $this->model->addToRecycle($id, 'AuthUserModel', '权限管理-用户管理', $this->currentUser['user_id'], $this->currentUser['user_type'], 'system', $this->currentUser['store_id']);
            return self::returnMsg(1);
        } else {
            return self::returnMsg(1002);
        }
    }

    /**
     * 保存状态
     * @param Request $request
     * @return \think\response\Json
     */
    public function onStatus(Request $request)
    {
        $formData = [
            'id' => $request->param('id'),
            'status' => $request->param('status'),
        ];
        $validate = 'app\validate\AuthUserValidate.upstatus';
        $result = $this->validate($formData, $validate);
        if (true !== $result) {
            return self::returnMsg(991, [], $result);
        } else {
            $ret = $this->model->saveData($formData);
            if (false !== $ret['flag']) {
                return self::returnMsg(1);
            } else {
                return self::returnMsg(0);
            }
        }
    }

    /**
     * 登录
     * @param Request $request
     * @return \think\response\Json
     */
    public function onLogin(Request $request)
    {
        $formData = [
            'username' => $request->param('username'),
            'password' => $request->param('password'),
            'type' => $request->param('type'),
            'phone' => $request->param('phone'),
            'code' => $request->param('code'),
            'captcha' => $request->param('captcha')
        ];
        if ($formData['type'] == 'phone') {
            $data = $this->model->loginPhone($formData);
            return self::returnMsg($data);
        }
        $result = $this->validate($formData, 'app\validate\AuthUserValidate.login');
        if (true !== $result) {
            return self::returnMsg(991, [], $result);
        } else {
            $data = $this->model->login($formData);
            return self::returnMsg($data);
        }
    }

    /**
     * 退出登录
     * @return \think\response\Json
     */
    public function onLogout()
    {
        $flag = $this->model->delRedis($this->currentUser['user_id'], '_store');
        if (false !== $flag) {
            return self::returnMsg(1);
        } else {
            return self::returnMsg(0);
        }
    }

    /**
     * 修改密码
     * @param $oldpwd
     * @param $newpwd
     * @return \think\response\Json
     */
    public function onEditpwd($oldpwd, $newpwd)
    {
        $aes = new Aes();
        $oldpwd = $aes->decrypt($oldpwd);
        $newpwd = $aes->decrypt($newpwd);
        $user = $this->model->where('id', $this->currentUser['user_id'])->find();
        if (!password_verify($oldpwd, $user['password'])) {
            return self::returnMsg(2007);
        } else {
            $formData = [
                'id' => $this->currentUser['user_id'],
                'password' => password_hash($newpwd, PASSWORD_BCRYPT)
            ];
            $flag = $this->model->saveData($formData);
            if (false !== $flag) {
                return self::returnMsg(1);
            } else {
                return self::returnMsg(0);
            }
        }
    }

    /**
     * 刷新token
     * @param $refresh_token
     * @return \think\response\Json
     */
    public function onToken($refresh_token)
    {
        $jwtKey = Config::get('system.jwt_key');
        $refreshToken = (array)JWT::decode($refresh_token, $jwtKey, array('HS256'));
        $storage = $this->model->getUserLoginStorage($this->currentUser['user_id'], '_store');
        if ($storage['refresh_token'] == $refresh_token) {
            $user_id = $refreshToken['user_id'];
            $token_set = Config::get('system.token_set');
            if ((time() - intval($refreshToken['create_time'])) < $token_set['admin']['refresh_expires']) {
                $access_token = JWT::encode(['user_id' => $user_id, 'create_time' => time(), 'expires_in' => $token_set['admin']['access_expires'], 'type' => 'access'], $jwtKey);
                $refresh_token = JWT::encode(['user_id' => $user_id, 'create_time' => time(), 'expires_in' => $token_set['admin']['refresh_expires'], 'type' => 'refresh'], $jwtKey);
                $storage['access_token'] = $access_token;
                $storage['refresh_token'] = $refresh_token;
                $this->model->setUserLoginStorage($storage, '_store');
                return self::returnMsg(1, [
                    'access_token' => $access_token,
                    'refresh_token' => $refresh_token
                ]);
            }
        }
        return self::returnMsg(2005);
    }

    /**
     * 删除用户缓存
     * @param string $id
     * @return \think\response\Json
     */
    public function onDelCache($id = '')
    {
        if (!empty($id)) {
            $flag = $this->model->delRedis($id);
        } else {
            $flag = $this->model->redis->del($this->model->redis_key);
        }
        if (false !== $flag) {
            return self::returnMsg(1);
        } else {
            return self::returnMsg(0);
        }
    }

}